package com.inossem.utils.download;

import android.content.Context;

import com.inossem.utils.base.Utils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadQueue;
import com.yanzhenjie.nohttp.download.DownloadRequest;
import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.ServerError;
import com.yanzhenjie.nohttp.error.StorageReadWriteError;
import com.yanzhenjie.nohttp.error.StorageSpaceNotEnoughError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;

import java.io.File;

/**
 * Created by leij on 2017/8/31.
 */

public class DownloadUtils {

    public static final int DOWNLOAD_APK_REQUEST_CODE = 8888;

    public static final String APK_FILE_NAME = "InossemAPK_";

    public static final String APK_SUFFIX_NAME = ".apk";

    private static DownloadUtils instance;

    private static DownloadQueue mDownloadQueue;

    private static int THREAD_NUMBER = 3;

    private static final String DOWNLOAD_ROOT_PATH = "Inossem_Download"; // 下载根目录名称

    private static final String DOWNLOAD_APK = "APK"; //安装包目录名称

    private DownloadUtils() {
        mDownloadQueue = NoHttp.newDownloadQueue(THREAD_NUMBER);
    }

    public static DownloadUtils getInstance() {
        if (instance == null) {
            synchronized (DownloadUtils.class) {
                if (instance == null) {
                    instance = new DownloadUtils();
                }
            }
        }
        return instance;
    }

    public void download(Context context, int what, DownloadRequest request, DownloadListener listener) {
        mDownloadQueue.add(what, request, listener);
    }

    public void stop() {
        if (mDownloadQueue != null) {
            mDownloadQueue.stop();
        }
    }

    public static boolean isApkExist(Context context, String fileName) {
        File file = new File(getDownloadApkPath(context), fileName);
        if (file.exists() && file.isFile()) {
            return true;
        } else {
            return false;
        }
    }

    public static DownloadRequest getApkDownloadRequest(Context context, String url, String fileName) {
        String folder = Utils.getRootPath(context).getAbsolutePath() + File.separator + DOWNLOAD_ROOT_PATH + File.separator + DOWNLOAD_APK;
        return new DownloadRequest(url, RequestMethod.GET, folder, fileName, true, true);
    }

    public static String getDownloadApkPath(Context context) {
        return Utils.getRootPath(context).getAbsolutePath() + File.separator + DOWNLOAD_ROOT_PATH + File.separator + DOWNLOAD_APK;
    }

    public static String getErroMessage(Exception exception) {
        String result;
        if (exception instanceof ServerError) {
            result = "服务器数据错误";
        } else if (exception instanceof NetworkError) {
            result = "网络不可用，请检查网络";
        } else if (exception instanceof StorageReadWriteError) {
            result = "存储卡错误，请检查存储卡";
        } else if (exception instanceof StorageSpaceNotEnoughError) {
            result = "存储空间不足";
        } else if (exception instanceof TimeoutError) {
            result = "下载超时";
        } else if (exception instanceof UnKnownHostError) {
            result = "找不到服务器";
        } else if (exception instanceof URLError) {
            result = "URL地址错误";
        } else {
            result = "未知错误";
        }
        return result;
    }

}
